package com.hosever.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hosever.core.AbstractController;
import com.hosever.core.model.Page;
import com.hosever.core.model.PageRequest;
import com.hosever.model.Article;
import com.hosever.model.Tag;
import com.hosever.service.ArticleService;
import com.hosever.utils.SeverHtmlUtils;

@Controller
@RequestMapping("/pub/article")
public class ArticleController extends AbstractController<Article, ArticleService> {
	
	@RequestMapping("/list")
	public String articleList(Model model,HttpSession session,PageRequest pageRequest) {
		session.setAttribute("nav", "article");
		Page<Article> articleList = this.service.page(pageRequest);
		Map<String, List<Object>> contentMap = new HashMap<String, List<Object>>();
		for(Article a :articleList.getContent()){
			List<Object> value = new ArrayList<Object>();
			value.add(SeverHtmlUtils.html2Text(a.getContent()));
			List<Tag> tags = this.service.findTagsById(a.getId());
			value.add(tags);
			contentMap.put(a.getId(), value);
		}
		model.addAttribute("page", articleList);
		model.addAttribute("contentMap", contentMap);
		
		
		return getRelativePath("/formList");
	}
	
	@RequestMapping("/view/{id}")
	public String articleView(@PathVariable("id") String id,Model model,HttpSession session) {
		String menuItem =  "article";
		session.setAttribute("nav", menuItem);
		Article article = this.service.findById(id);
		model.addAttribute("article", article);
		List<Tag> tags = this.service.findTagsById(article.getId());
		model.addAttribute("tags", tags);
		return getRelativePath("/formView");
	}
}









